package vip.xiaonuo.smzq.modular.kcl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.mapper.Cb08XzqhMapper;
import vip.xiaonuo.smzq.modular.kcl.entity.*;
import vip.xiaonuo.smzq.modular.kcl.entity.param.ClspParam;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.ClspVo;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KcclSpJg;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.SplcVo;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.Zlxqcx;
import vip.xiaonuo.smzq.modular.kcl.mapper.ClspMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.ClspczrzMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.WjglbMapper;
import vip.xiaonuo.smzq.modular.kcl.mapper.ZlscMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IClspService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.kcl.service.IKsService;
import vip.xiaonuo.smzq.modular.kcl.service.ISjyjService;
import vip.xiaonuo.smzq.modular.lifecycle.service.module.LifeCycleHanderDataService;
import vip.xiaonuo.smzq.modular.user.entity.Org;
import vip.xiaonuo.smzq.modular.user.entity.Position;
import vip.xiaonuo.smzq.modular.user.entity.Role;
import vip.xiaonuo.smzq.modular.user.entity.User;
import vip.xiaonuo.smzq.modular.user.mapper.OrgMapper;
import vip.xiaonuo.smzq.modular.user.mapper.PositionMapper;
import vip.xiaonuo.smzq.modular.user.mapper.RoleMapper;
import vip.xiaonuo.smzq.modular.user.mapper.UserMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 储量审批流程 服务实现类
 *
 * @author sqq
 * @since 2024-04-23
 */
@Service
public class ClspServiceImpl extends ServiceImpl<ClspMapper, Clsp> implements IClspService {

    @Resource
    private ClspMapper clspMapper;
    @Resource
    private OrgMapper orgMapper;//级别部省市县填报
    @Resource
    private PositionMapper positionMapper;//所对应的行政区划
    @Resource
    private UserMapper userMapper;//用户
    @Resource
    private Cb08XzqhMapper cb08XzqhMapper;//行政区划
    @Resource
    private IKsService iKsService;//矿山
    @Resource
    private ISjyjService iSjyjService;//数据预警

    @Resource
    private RoleMapper roleMapper;//角色

    @Resource
    private ZlscMapper zlscMapper;
    @Resource
    private WjglbMapper wjglbMapper;
    @Resource
    private ClspczrzMapper clspczrzMapper;//审批操作日志
    @Autowired
    LifeCycleHanderDataService lifeCycleHanderDataService;

    @Override
    public Page<Clsp> page(Clsp clsp) {
        QueryWrapper<Clsp> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public Page<ClspVo> clspVopage(long current, long size, Integer nd, String xzqh, String tbzt, String spzt) {
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        Role position = roleMapper.selectById(user.getPositionId());
        String code = position.getCode();
        Page<Clsp> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);

        List<Clsp> splcList = new ArrayList<>();
        List<ClspVo> splcVoList = new ArrayList<>();
        Page<ClspVo> splcVoPage = new Page<>();

        QueryWrapper<Clsp> queryWrapper = new QueryWrapper<>();

        if (xzqh == null) {
            if (user.getOrgId().equals("-1") || user.getOrgId().length() > 8) {
                xzqh = "130000";
            } else {
                xzqh = user.getOrgId();
            }
        }
        if (tbzt != null) {
            if (tbzt.equals("1")) {
                tbzt = "已填报";
            } else {
                tbzt = "未填报";
            }

            queryWrapper.eq("tbzt", tbzt);
        }


        if (xzqh != null) {
            xzqh = xzqh.replaceAll("0+$", "");
            queryWrapper.like("xian", xzqh);
        }
        if (nd != null) {
            queryWrapper.eq("nd", nd);
        }


        if (code.equals("shengsh")) {//省
            if (spzt != null) {
                if (spzt.equals("0")) {
                    queryWrapper.in("spzt", "1", "2", "3", "6");
                } else if (spzt.equals("1")) {
                    queryWrapper.eq("spzt", "5");
                } else {
                    queryWrapper.in("spzt", "6", "7", "8");
                }
            } else {
                queryWrapper.in("spzt", "0", "1", "2", "3", "5", "6", "7", "8", "9", "10");
            }

        } else if (code.equals("shish")) { //市
            if (spzt != null) {
                if (spzt.equals("0")) {
                    queryWrapper.in("spzt", "1", "2", "6");
                } else if (spzt.equals("1")) {
                    queryWrapper.in("spzt", "3", "5");
                } else {
                    queryWrapper.in("spzt", "8", "9");
                }
            } else {
                queryWrapper.in("spzt", "0", "1", "2", "3", "5", "6", "7", "8", "9", "10");
            }

        } else if (code.equals("xiansh")) {//县
            if (spzt != null) {
                if (spzt.equals("0")) {
                    queryWrapper.in("spzt", "1", "6");
                } else if (spzt.equals("1")) {
                    queryWrapper.in("spzt", "2", "3", "5");
                } else {
                    queryWrapper.in("spzt", "9", "10");
                }
            } else {
                queryWrapper.in("spzt", "0", "1", "2", "3", "5", "6", "7", "8", "9", "10");
            }
        }

//        else if (code.equals("bjsh")) {//部
//            if (spzt != null){
//                if (spzt.equals("0")){
//                    queryWrapper.eq("spzt", "2");
//                }else{
//                    queryWrapper.eq("spzt", "3");
//                }
//            }else{
//                queryWrapper.in("spzt", "3", "5");
//            }
//
        else if (code.equals("zt7qwacif0")) {//矿业权人

            if (spzt != null) {
                if (spzt.equals("0")) {
                    queryWrapper.in("spzt", "0", "1", "6");
                } else if (spzt.equals("1")) {
                    queryWrapper.in("spzt", "2", "3", "5");
                } else {
                    queryWrapper.in("spzt", "0");
                }
            } else {
                queryWrapper.in("spzt", "0", "1", "2", "3", "5", "6", "7", "8", "9", "10");
            }

            queryWrapper.eq("creat_by", saBaseLoginUser.getId());
        }
        queryWrapper.orderByAsc("gxsj");
        queryWrapper.orderByAsc("spzt");
        queryWrapper.orderByDesc("cteat_time");

        page = clspMapper.selectPage(page, queryWrapper);

        splcList = page.getRecords();


        for (Clsp clsp : splcList) {
            ClspVo clspVo = new ClspVo();
            BeanUtil.copyProperties(clsp, clspVo);

            //县
            QueryWrapper<Cb08Xzqh> xianWrapper = new QueryWrapper<>();
            xianWrapper.eq("dm", Integer.parseInt(clsp.getXian()));
            Cb08Xzqh xian = cb08XzqhMapper.selectOne(xianWrapper);
            clspVo.setXianMc(xian.getMc());
            //市
            QueryWrapper<Cb08Xzqh> shiWrapper = new QueryWrapper<>();
            shiWrapper.eq("dm", Integer.parseInt(clsp.getShi()));
            Cb08Xzqh shi = cb08XzqhMapper.selectOne(shiWrapper);
            clspVo.setShiMc(shi.getMc());
            //省
            QueryWrapper<Cb08Xzqh> shengWrapper = new QueryWrapper<>();
            shengWrapper.eq("dm", Integer.parseInt(clsp.getSheng()));
            Cb08Xzqh sheng = cb08XzqhMapper.selectOne(shengWrapper);
            clspVo.setShengMc(sheng.getMc());

            int approvalStatus = Integer.parseInt(clsp.getSpzt());
            if (approvalStatus == 0) {
                // 未审批
                clspVo.setSpztMc("未审核");
            } else if (approvalStatus == 1) {
                // 县审批
                clspVo.setSpztMc("县审核");
            } else if (approvalStatus == 2) {
                // 市审批
                clspVo.setSpztMc("市审核");
            } else if (approvalStatus == 3) {
                // 省审批
                clspVo.setSpztMc("省审核");
            } else if (approvalStatus == 4) {
                // 部审批
                clspVo.setSpztMc("部审核");
            } else if (approvalStatus == 5) {
                // 审批完成
                clspVo.setSpztMc("审核完成");
            } else if (approvalStatus == 6) {
                clspVo.setSpztMc("部回退");
            } else if (approvalStatus == 7) {
                clspVo.setSpztMc("省回退");
            } else if (approvalStatus == 8) {
                clspVo.setSpztMc("市回退");
            } else if (approvalStatus == 9) {
                clspVo.setSpztMc("县回退");
            } else if (approvalStatus == 10) {
                clspVo.setSpztMc("待修改");
            }


//            User userKqrmc = userMapper.selectById(clsp.getKyqr());
//            clspVo.setKqrmc(userKqrmc.getName());
            clspVo.setKqrmc(clsp.getKyqr());

            List<Clspczrz> clspczrzList = new ArrayList<>();
            LambdaQueryWrapper<Clspczrz> czrzWrapper = new LambdaQueryWrapper<>();
            czrzWrapper.eq(Clspczrz::getNbspid, clsp.getPkid());
            czrzWrapper.orderByDesc(Clspczrz::getCzsj);
            List<Clspczrz> clspczrzListSel = clspczrzMapper.selectList(czrzWrapper);
            if (clspczrzListSel.size() > 0) {
                clspczrzList = clspczrzListSel;
            }
            clspVo.setClspczrzList(clspczrzList);
            splcVoList.add(clspVo);
        }
        splcVoPage.setCurrent(page.getCurrent());
        splcVoPage.setSize(page.getSize());
        splcVoPage.setRecords(splcVoList);
        splcVoPage.setTotal(page.getTotal());
        return splcVoPage;
    }

    @Override
    public void updateClspVo(ClspVo clspVo) {
        Clsp clsp = new Clsp();
        BeanUtil.copyProperties(clspVo, clsp);
        //操作日志
        Clspczrz clspczrz = new Clspczrz();
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        //五级账户
        Role position = roleMapper.selectById(user.getPositionId());
        String code = position.getCode();
//        clsp.setSpjg("0");
        if (clsp.getSpjg().equals("0")) {
            String positionCode = "";
            if (user.getOrgId().equals("-1") || user.getOrgId().length() > 8) {
                positionCode = "130000";
            } else {
                positionCode = user.getOrgId();
            }
            positionCode = positionCode.replaceAll("0+$", "");
            if (positionCode.length() > 4 && clsp.getSpzt().equals("1")) {
                clsp.setSpzt("2");
            } else if (positionCode.length() > 2 && clsp.getSpzt().equals("2")) {
                clsp.setSpzt("3");
            } else if (positionCode.length() == 2 && clsp.getSpzt().equals("3")) {
                clsp.setSpzt("5");
                //同步生命周期，推送年报节点
                pushAnnualReport(clspVo.getKqbh(), clspVo.getDjflbm(), clspVo.getNd());
                //同步数据    clsp.getNd(),clsp.getKqbh(),clsp.getDjflbm()
                boolean ifSuccessNextYear = iKsService.generateAnnualReportTask(clsp.getNd(), clsp.getKqbh(), clsp.getDjflbm());
                boolean ifSuccessAfterNext = iKsService.generateAnnualReportTask((clsp.getNd() + 1), clsp.getKqbh(), clsp.getDjflbm());

                //数据同步成功，下一年度填报任务发布 便添加一条填报预警
                if (ifSuccessNextYear) {
                    if (clsp.getDjflbm() > 2000 && clsp.getDjflbm() < 3000) {
                        iSjyjService.addSjyj(clsp.getNd(), clsp.getDjflbm(), clsp.getKqbh(), 308002);
                    } else {
                        iSjyjService.addSjyj(clsp.getNd(), clsp.getDjflbm(), clsp.getKqbh(), 308001);
                    }

                    Clsp clspNew = new Clsp();
                    BeanUtil.copyProperties(clsp, clspNew);
                    //添加审批记录
                    addClsp(clspNew);
                }
//                }
            }
            clspczrz.setCzlx("通过审批");
        } else if (code.equals("zt7qwacif0") && clsp.getSpzt().equals("9")) {//矿业权人重新提交后将审批状态改为1，重新提交把结果改为0通过
            clsp.setSpzt("1");
            clsp.setSpjg("0");
            clspczrz.setCzlx("回填年报");
        } else if (clsp.getSpjg().equals("1") && clsp.getSpzt().equals("7")) { //回退之后，审批状态改为7，审批结果改为1未通过
            clsp.setSpzt("8");
            clsp.setSpjg("1");
            clspczrz.setCzlx("省退回");
        } else if (clsp.getSpjg().equals("1") && clsp.getSpzt().equals("8")) { //回退之后，审批状态改为8，审批结果改为1未通过
            clsp.setSpzt("9");
            clsp.setSpjg("1");
            clspczrz.setCzlx("市退回");
        } else if (clsp.getSpjg().equals("1") && clsp.getSpzt().equals("9")) { //回退之后，审批状态改为9，审批结果改为1未通过
            clsp.setSpzt("10");
            clsp.setSpjg("1");
            clspczrz.setCzlx("县退回");
        } else {
            throw new CommonException("不允许操作");
        }

        if (clspVo.getYj() != null) {
            clspczrz.setYj(clspVo.getYj());
        }
        //组织数据进行操作日志的补充
        clspczrz.setCzrid(saBaseLoginUser.getId());
        clspczrz.setCzrxm(saBaseLoginUser.getName());
        clspczrz.setCzsj(DateUtil.format(new Date(), "YYYY-MM-dd HH-mm-ss"));
        clspczrz.setNbspid(clsp.getPkid());
        clspczrzMapper.insert(clspczrz);

        clsp.setGxsj(DateUtil.format(new Date(), "YYYYMMddHHmmss"));
        //更新审批状态
        clspMapper.updateById(clsp);

    }


    /**
     * 推送生命周期年报节点
     *
     * @param nd
     * @param kqbh
     * @param djflbh
     * @return
     */

    public Boolean pushAnnualReport(String kqbh, Integer djflbh, Integer nd) {
        Boolean isSuccess = false;
        List<Zlxqcx> zlxqcxList = new ArrayList<>();
        try {
            QueryWrapper<Zlsc> zlscQuery = new QueryWrapper<>();
            zlscQuery.eq("djflbh", djflbh).eq("kqbh", kqbh).eq("nd", nd)
                    .eq("zlxz", 300011);
            zlscQuery.orderByDesc("zlsj");
            zlscQuery.last("limit 1");
            Zlsc zlsc = zlscMapper.selectOne(zlscQuery);
            QueryWrapper<Wjglb> wjglbQuery = new QueryWrapper<>();
            wjglbQuery.eq("zlsc_id", zlsc.getPkid());
            wjglbQuery.eq("djflbh", djflbh).eq("kqbh", kqbh).eq("nd", nd);
            wjglbQuery.last("limit 1");
            Wjglb wjglb = wjglbMapper.selectOne(wjglbQuery);
            Zlxqcx zlxqcx = new Zlxqcx();
            zlxqcx.setGuanlianId(zlsc.getPkid());
            zlxqcx.setPkid(wjglb.getPkid());
            //矿业权库类型
            zlxqcx.setKyqklx(zlsc.getKyqklx());
            //矿业权登记类型
            zlxqcx.setKyqdjlx(zlsc.getKyqdjlx());
            //节点类型
            zlxqcx.setJdlx(zlsc.getJdlx());
            //资料性质
            zlxqcx.setZlxz(zlsc.getZlxz());
            zlxqcx.setWjbh(wjglb.getWjbh());
            zlxqcx.setWjmc(wjglb.getWjmc());
            zlxqcx.setWjhz(wjglb.getWjhz());
            zlxqcx.setWjlj(wjglb.getWjlj());
            zlxqcx.setKqbh(wjglb.getKqbh());
            zlxqcx.setDjflbh(wjglb.getDjflbh());
            zlxqcx.setNd(wjglb.getNd());
            zlxqcx.setZlsxh(wjglb.getZlsxh());
            zlxqcx.setZlsj(zlsc.getZlsj());
            zlxqcxList.add(zlxqcx);

            lifeCycleHanderDataService.saveLifeCycleData(zlxqcxList);

            isSuccess = true;

        } catch (Exception e) {

        }
        return isSuccess;

    }


    @Override
    public KcclSpJg getKcclSpJg(Integer nd, String xzqh) {
        KcclSpJg kcclSpJg = new KcclSpJg();
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
        //五级账户
        Role position = roleMapper.selectById(user.getPositionId());
        String code = position.getCode();

        if (xzqh == null) {
            if (user.getOrgId().equals("-1") || user.getOrgId().length() > 8) {
                xzqh = "130000";
            } else {
                xzqh = user.getOrgId();
            }
        }

// 处理 xzqh 条件
        if (xzqh == null) {
            if ("-1".equals(user.getOrgId()) || user.getOrgId().length() > 8) {
                xzqh = "130000";
            } else {
                xzqh = user.getOrgId();
            }
        }
        if (xzqh != null) {
            xzqh = xzqh.replaceAll("0+$", ""); // 去掉末尾的 0
        }


// 查询已填报和未填报的数量
        kcclSpJg.setYtb(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                .like(xzqh != null, Clsp::getXian, xzqh)
                .eq(nd != null, Clsp::getNd, nd)
                .eq(Clsp::getTbzt, "已填报")));

        kcclSpJg.setWtb(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                .like(xzqh != null, Clsp::getXian, xzqh)
                .eq(nd != null, Clsp::getNd, nd)
                .eq(Clsp::getTbzt, "未填报")));

// 根据 code 值动态添加审批状态条件
        if ("zt7qwacif0".equals(code)) { // 超管
            kcclSpJg.setYsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .eq(Clsp::getCreatBy, saBaseLoginUser.getId())
                    .in(Clsp::getSpzt, Arrays.asList("2", "3", "5"))));

            kcclSpJg.setWsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .eq(Clsp::getCreatBy, saBaseLoginUser.getId())
                    .in(Clsp::getSpzt, Arrays.asList("0", "1", "6"))));

            kcclSpJg.setHtxg(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .eq(Clsp::getCreatBy, saBaseLoginUser.getId())
                    .in(Clsp::getSpzt, Arrays.asList("10"))));

        } else if ("shengsh".equals(code)) { // 省
            kcclSpJg.setYsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("5"))));

            kcclSpJg.setWsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("1", "2", "3", "6"))));

            kcclSpJg.setHtxg(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("7", "8"))));

        } else if ("shish".equals(code)) { // 市
            kcclSpJg.setYsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("3", "5"))));

            kcclSpJg.setWsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("1", "2", "6"))));

            kcclSpJg.setHtxg(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("8", "9"))));

        } else if ("xiansh".equals(code)) { // 县
            kcclSpJg.setYsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("2", "3", "5"))));

            kcclSpJg.setWsp(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("1", "6"))));

            kcclSpJg.setHtxg(clspMapper.selectCount(new MPJLambdaWrapper<Clsp>()
                    .like(xzqh != null, Clsp::getXian, xzqh)
                    .eq(nd != null, Clsp::getNd, nd)
                    .in(Clsp::getSpzt, Arrays.asList("9", "10"))));
        }


        return kcclSpJg;
    }

    @Override
    public void addKcclSpJg(Integer nd) {
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        List<Ks> ksList = iKsService.selectList(new QueryWrapper<Ks>().eq("nd", nd));
        for (Ks ks : ksList) {
            //添加明年年报数据
            boolean ifSuccess = iKsService.generateAnnualReportTask(ks.getNd(), ks.getKqbh(), ks.getDjflbh());
            //添加后年年报数据
            boolean ifSuccessNext = iKsService.generateAnnualReportTask((ks.getNd() + 1), ks.getKqbh(), ks.getDjflbh());
            Clsp clsp = new Clsp();
            if (ks.getKqbh() != null) {
                clsp.setXkzh(ks.getXkzh());
            }
            clsp.setSpjg("0");
            clsp.setNd(nd + 1);
            clsp.setDjflbm(ks.getDjflbh());
            clsp.setKqbh(ks.getKqbh());
            if (ks.getKsbh() != null) {
                clsp.setKsbh(ks.getKsbh());
            }
            //县
            clsp.setXian(String.valueOf(ks.getXzqdm()));
            //市
            QueryWrapper<Cb08Xzqh> shiWrapper = new QueryWrapper<>();
            shiWrapper.eq("dm", ks.getXzqdm());
            Cb08Xzqh shi = cb08XzqhMapper.selectOne(shiWrapper);
            clsp.setShi(String.valueOf(shi.getFdm()));
            //省
            QueryWrapper<Cb08Xzqh> shengWrapper = new QueryWrapper<>();
            shengWrapper.eq("dm", shi.getFdm());
            Cb08Xzqh sheng = cb08XzqhMapper.selectOne(shengWrapper);
            clsp.setSheng(String.valueOf(sheng.getFdm()));
            //矿权人
            if (ks.getKyqr() != null) {
                clsp.setKyqr(ks.getKyqr());
            }
            //创建人
            clsp.setCreatBy(saBaseLoginUser.getId());
            // clsp.setKyqr(saBaseLoginUser.getId());
            addClsp(clsp);
        }
    }

    /**
     * 年报审批通过提交下一年的未填报数据
     *
     * @param clsp
     */
    public void addClsp(Clsp clsp) {
        //创建时间
        clsp.setCteatTime(DateUtil.format(new Date(), "YYYYMMdd"));
        clsp.setTbzt("未填报");
        clsp.setSpzt("0");
        clsp.setNd(clsp.getNd() + 1);
        clsp.setPkid("");
        //  clsp.setNd(clsp.getNd()+1);
        //查询数据库，是否已经有审批记录若有则不在新增，否则新增
        QueryWrapper<Clsp> clspWrapper = new QueryWrapper<>();
        clspWrapper.eq("kqbh", clsp.getKqbh());
        clspWrapper.eq("djflbm", clsp.getDjflbm());
        clspWrapper.eq("nd", clsp.getNd() + 1);
        List<Clsp> clspLast = this.list(clspWrapper);


        if (clspLast == null || clspLast.size() == 0) {
            this.save(clsp);
        }
    }

    /**
     * 判断年报是否有储量审批结果
     */
    @Override
    public Boolean getKcclSpJgCount(ClspParam param) {
        boolean result = false;
        LambdaQueryWrapper<Clsp> clspWrapper = new LambdaQueryWrapper<>();
        clspWrapper.eq(Clsp::getKqbh, param.getKqbh());
        clspWrapper.eq(Clsp::getDjflbm, param.getDjflbm());
        clspWrapper.eq(Clsp::getNd, param.getNd());
        clspWrapper.last("limit 1");
        Clsp clsp = this.getOne(clspWrapper);
        String spjg = "0,10";
        if (clsp != null && !spjg.contains(clsp.getSpzt())) {
            result = true;
        }
        return result;
    }
}
